package com.tank.thread;

import java.io.UnsupportedEncodingException;

/**
 * User: suqier Date:2016/11/3 ProjectName: asias-icpaygate Version: 1.0
 **/
public class ChineseAreaCode {

    public class ToomuchWordException extends Exception{

        private static final long serialVersionUID = 1L;

        public ToomuchWordException(){}

        public ToomuchWordException(String message){
            super(message);
        }
    }
    /*
     * @function get the area code Chinese word
     * @param word
     * the single Chinese word to deal
     * @UnsupportedEncodingException
     * throws when word is not supported
     * @ToomuchWordException
     * throws when word is not a single word*/
    public String toAreaCode(String word) throws UnsupportedEncodingException,ToomuchWordException{
        if(word.length()!=1){
            /*word is not a single word*/
            throw new ToomuchWordException();
        }
        byte[] bs=word.getBytes("GB2312");
        String areaCode="";
        for(byte b:bs){
            int code=Integer.parseInt(Integer.toHexString(b & 0xff),16);
            areaCode += (code-0x80-0x20);       /*transfer the machine code to area code*/
        }
        return areaCode;
    }

    public static void main(String[] args) throws UnsupportedEncodingException, ToomuchWordException{
//        String word="唐";
//        ChineseAreaCode cac = new ChineseAreaCode();
//        String qwm = cac.toAreaCode(word);
//        String hex = bytesToHex(qwm.getBytes());
//        Long x = Long.parseLong(hex,16);
//        Long y = Long.parseLong("80",16);
//        // 44 38
//        Long qm = Long.parseLong("44",16);
//        qm += 80;
//        Long wm = Long.parseLong("38",16);
//        wm += 80;
//
//        System.out.println("hex:{}"+Long.toHexString(qm+wm));
//        System.out.println("bytesToHex:"+bytesToHex(word.getBytes()));
        System.out.println(Integer.toHexString(999999999));
    }

    public static String bytesToHex(byte[] in) {
        final StringBuilder builder = new StringBuilder();
        for(byte b : in) {
            builder.append(String.format("%02x", b));
        }
        return builder.toString();
    }
}

